

package org.koyabr.webx.demo.danmaku.dal.dao.ibatis;

import static com.alibaba.citrus.util.StringUtil.*;

import org.koyabr.webx.demo.danmaku.dal.dao.UserDao;
import org.koyabr.webx.demo.danmaku.dal.dataobjs.User;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class IbatisUserDao extends SqlMapClientDaoSupport implements UserDao {
    public User getUserByName(String username) {
        return (User) getSqlMapClientTemplate().queryForObject("getUserByUsername", username);
    }

    public User getAuthenticatedUser(String userId, String password) {
        User user = new User();

        user.setUsername(userId);
        user.setPassword(password);

        return (User) getSqlMapClientTemplate().queryForObject("getUserByUsernameAndPassword", user);
    }


    public void insertUser(User user) {
        getSqlMapClientTemplate().insert("insertUser", user);
        getSqlMapClientTemplate().insert("insertProfile", user);
    }

    public void updateUser(User user) {
        getSqlMapClientTemplate().update("updateProfile", user);

        if (!isEmpty(user.getPassword())) {
            getSqlMapClientTemplate().update("updateUser", user);
        }
    }
}
